package org.zfm.base.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zfm.base.mapper.BaseMapper;
import org.zfm.base.BaseQuery;
import org.zfm.base.service.IBaseService;
import org.zfm.util.data.PageData;

import java.io.Serializable;
import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;
    @Override
    @Transactional
    public void insert(T t) {
        baseMapper.insert(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Serializable id) {
        baseMapper.delete(id);
    }

    @Override
    public PageData<T> queryPageData(BaseQuery query) {
        Long total = baseMapper.queryTotal(query);
        if(total<1){
            return new PageData<>();
        }
        List<T> tList =(List<T>) baseMapper.queryList(query);
        return new PageData<T>(total,tList);
    }

    @Override
    public List<T> selectAll() {
        return baseMapper.selectAll();
    }

    @Override
    public T selectById(Serializable id) {
        return (T) baseMapper.selectById(id);
    }

    @Override
    public Integer batchDelete(List<Long> ids) {
        return baseMapper.batchDelete(ids);
    }
}
